package com.hy.mydesktop.client.component.mvc.factory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.data.BaseModel;

import com.extjs.gxt.ui.client.mvc.Controller;

import com.hy.mydesktop.client.component.mvc.factory.util.ControllerFactoryUtil;
import com.hy.mydesktop.client.component.type.ComponentControllerEnum;
import com.hy.mydesktop.client.mvc.base.BaseController;
import com.hy.mydesktop.shared.persistence.domain.gxt.GxtComponentControllerMetaModel;
import com.hy.mydesktop.shared.persistence.domain.gxt.GxtComponentControllerMetaNodeModel;
import com.hy.mydesktop.shared.persistence.domain.gxt.constant.GxtMetaModelTypeEnum;
import com.hy.mydesktop.shared.rpc.util.ComponentControllerEnumConverter;
import com.hy.mydesktop.shared.util.log.MyLoggerUtil;




/**
 * 
 * <ul>
 * <li>开发作者：花宏宇</li>
 * <li>设计日期：2010-8-27；时间：上午11:52:08</li>
 * <li>类型名称：GxtComponentControlerFactory</li>
 * <li>设计目的：</li>
 * </ul>
 * <ul>
 * <b>修订编号：</b>
 * <li>修订日期：</li>
 * <li>修订作者：</li>
 * <li>修订原因：</li>
 * <li>修订内容：</li>
 * </ul>
 */
public  class GxtComponentControlerFactory {
	
	private static final String className = "com.hy.mydesktop.client.component.mvc.factory.GxtComponentControlerFactory";
	/**
	 * 
	 * <ul>
	 * <li>方法含义：GxtComponentMetaModel信息，创建控件。<br>
	 * 注；GxtComponentMetaModel必须是树形结构(只有一个根节点)
	 * </li>
	 * <li>方法作者：花宏宇</li>
	 * <li>编写日期：2010-8-28；时间：下午上午10:56:34</li>
	 * </ul>
	 * <ul>
	 * <b>修订编号：</b>
	 * <li>修订日期：</li>
	 * <li>修订作者：</li>
	 * <li>修订原因：</li>
	 * <li>修订内容：</li>
	 * </ul>
	 * @param <COMPONENT>
	 * @param gxtComponentControllerMetaModel
	 * @return
	 */
	public static <CONTROLLER extends Controller> CONTROLLER createGxtComponentController(
			GxtComponentControllerMetaModel gxtComponentControllerMetaModel) {
		
		
		GxtMetaModelTypeEnum gxtMetaModelTypeEnum = gxtComponentControllerMetaModel.getGxtControllerMetaModelTypeEnum();
		switch (gxtMetaModelTypeEnum) {
			case BASE_GXT_CONTROLLER_META_MODEL_TYPE:
				
				return createBaseGxtComponentController(gxtComponentControllerMetaModel.getRoot());
				
			case TREE_GXT_CONTROLLER_META_MODEL_TYPE:
				
				return createTreeGxtComponentController(gxtComponentControllerMetaModel);
			default:
				return null;
		}
	}
	
	public static <CONTROLLER extends Controller> CONTROLLER createOneGxtComponentController(
			GxtComponentControllerMetaNodeModel gxtComponentControllerMetaNodeModel) {
		BaseController singleController =  createBaseGxtComponentController(gxtComponentControllerMetaNodeModel);
  		ControllerFactoryUtil.reConfigureBaseController(singleController);

		return (CONTROLLER)singleController;
		//return createBaseGxtComponentController(gxtComponentControllerMetaNodeModel);
	}
	
	/**
	 * 根据GxtComponentMetaModel(继承BaseModel)，创建单一控件
	 * @param <CONTROLLER>
	 * @param gxtComponentControllerMetaNodeModel
	 * @return
	 */
	private static <CONTROLLER extends Controller> CONTROLLER createBaseGxtComponentController(
			GxtComponentControllerMetaNodeModel gxtComponentControllerMetaNodeModel) {
		//BaseModel gxtComponentMetaModel=null;
		
		CONTROLLER controller = null;
				
		
		ComponentControllerEnum componentControllerEnum = gxtComponentControllerMetaNodeModel.getComponentControllerEnum();
		if (componentControllerEnum==null) {
			ComponentControllerEnumConverter.updateComponentControllerEnum(gxtComponentControllerMetaNodeModel);
			componentControllerEnum = gxtComponentControllerMetaNodeModel.getComponentControllerEnum();
			if (componentControllerEnum==null) {
				MyLoggerUtil.printError(className, "没有查询到componentControllerEnum类型");
			}else {
				MyLoggerUtil.printDebug(className, " gxtComponentControllerMetaNodeModel.getComponentControllerEnumIndex() is "
					+gxtComponentControllerMetaNodeModel.getComponentControllerEnumIndex()+"("+componentControllerEnum.name()+"; gxtComponentControllerMetaNodeModel.getComponentControllerEnum() is null ;)");
			}
		
		}
		
		
		switch (componentControllerEnum) {
		//逐一枚举出ComponentTypeEnum中所有的控件类型
			case TREEPANELMODULES_CONTROLLER_MODEL: {
				controller = (CONTROLLER) TreePanelModulesControllerFactory.createTreePanelController(gxtComponentControllerMetaNodeModel);
				break;
			}
			
			case GRIDSETMODULES_CONTROLLER_MODEL: {
				controller = (CONTROLLER) GridSetModulesControllerFactory.createGridSetModulesController(gxtComponentControllerMetaNodeModel);
				break;
			}
						
			
			case DATALOAD_CONTROLLER_MODEL: {
				controller = (CONTROLLER) DataLoadControllerFactory.createDataLoadController(gxtComponentControllerMetaNodeModel);
				break;
			}
			
			case GRID_DATALOAD_CONTROLLER_MODEL: {
				controller = (CONTROLLER) GridDataLoadControllerFactory.createGridDataLoadController(gxtComponentControllerMetaNodeModel);
				break;
			}
			
			case GRID_CONTROLLER_MODEL: {
				controller = (CONTROLLER) GridControllerFactory.createGridController(gxtComponentControllerMetaNodeModel);
				break;
			}
			
			case DATEFIELD_CONTROLLER_MODEL: {
				controller = (CONTROLLER) DateFieldControllerFactory.createDateFieldController(gxtComponentControllerMetaNodeModel);
				break;
			}
			//下拉列表控制器
			case COMBOBOX_CONTROLLER_MODEL: {
				controller = (CONTROLLER) ComboBoxControllerFactory.createComboBoxController(gxtComponentControllerMetaNodeModel);
				break;
			}
			//简单下拉列表控制器
			case SIMPLECOMBOBOX_CONTROLLER_MODEL: {
				controller = (CONTROLLER) SimpleComboBoxControllerFactory.createSimpleComboBoxController(gxtComponentControllerMetaNodeModel);
				break;
			}
			//复选框控制
			case CHECKGROUP_CONTROLLER_MODEL: {
				controller = (CONTROLLER) CheckBoxGroupControllerFactory.createCheckBoxGroupController(gxtComponentControllerMetaNodeModel);
				break;
			}
			//单选框控制器
			case RADIOGROUP_CONTROLLER_MODEL: {
				controller = (CONTROLLER) RadioGroupControllerFactory.createRadioGroupController(gxtComponentControllerMetaNodeModel);
				break;
			}
			
			case FORMSET_MODULE_CONTROLLER_MODEL: {
				controller = (CONTROLLER) FormSetModulesControllerFactory.createFormSetModulesController(gxtComponentControllerMetaNodeModel);
				break;
			}
			
			case FORMMODULE_CONTROLLER_MODEL: {
				controller = (CONTROLLER) FormModuleControllerFactory.createFormModuleController(gxtComponentControllerMetaNodeModel);
				break;
			}
			
			case DESKTOP_CONTROLLOR_MODEL: {
				controller = (CONTROLLER) DesktopControllerFactory.createDesktopController(gxtComponentControllerMetaNodeModel);
				break;
			}
			
			case FORMPANEL_CONTROLLER_MODEL: {
				controller = (CONTROLLER) FormPanelControllerFactory.createFormPanelController(gxtComponentControllerMetaNodeModel);
				break;
			}
			
			case TEXTFIELD_CONTROLLER_MODEL: {
				controller = (CONTROLLER) TextFieldControllerFactory.createTextFieldController(gxtComponentControllerMetaNodeModel);
				break;
			}
			
			case TEXTAREA_CONTROLLER_MODEL: {
				controller = (CONTROLLER) TextFieldControllerFactory.createTextFieldController(gxtComponentControllerMetaNodeModel);
				break;
			}
			
			case TOOLBAR_CONTROLLER_MODEL: {
				controller = (CONTROLLER) ToolBarControllerFactory.createToolBarController(gxtComponentControllerMetaNodeModel);
				break;
			}
			
			case MENUBAR_CONTROLLER_MODEL: {
				controller = (CONTROLLER) MenuBarControllerFactory.createMenuBarController(gxtComponentControllerMetaNodeModel);
				break;
			}
			
			
			case SHORTCUT_CONTROLLER_MODEL: {
				controller = (CONTROLLER) ShortcutControllerFactory.createShortcutController(gxtComponentControllerMetaNodeModel);
				break;
			}
			
			case DESKTOP_MENUITEM_CONTROLLER_MODEL: {
				controller = (CONTROLLER) DesktopMenuItemControllerFactory.createDesktopMenuItemController(gxtComponentControllerMetaNodeModel);
				break;
			}
			
			case DESKTOP_STARTMENU_CONTROLLER_MODEL: {
				controller = (CONTROLLER) DesktopStartMenuControllerFactory.createDesktopStartMenuController(gxtComponentControllerMetaNodeModel);
				break;

			}
			//窗口控制器
			case WINDOW_CONTROLLER_MODEL: {
				controller = (CONTROLLER) WindowControllerFactory.createWindowController(gxtComponentControllerMetaNodeModel);
				break;
			}
			
			//内容面板控制器
			case CONTENTPANEL_CONTROLLER_MODEL: {
				controller = (CONTROLLER) ContentPanelControllerFactory.createContentPanelController(gxtComponentControllerMetaNodeModel);
				break;
			}
			//Tab面板控制器
			case TABPANEL_CONTROLLER_MODEL: {
				controller = (CONTROLLER) TabPanelControllerFactory.createTabPanelController(gxtComponentControllerMetaNodeModel);
				break;
			}
			//TabItem内容面板控制器
			case TABITEM_CONTROLLER_MODEL: {
				controller = (CONTROLLER) TabItemControllerFactory.createTabItemController(gxtComponentControllerMetaNodeModel);
				break;
			}
			default: {//当componentTypeEnum为空，或者与现有控件类型不符合时
				controller = null;
				//MyLoggerUtil.printWarn(className, "没有发现匹配的Controller!!!");
				MyLoggerUtil.printError(className, "没有发现匹配的Controller!!!");
				break;
			}
			}
			return controller;
	}
	
	/**
	 * 
	 * <ul>
	 * <li>方法含义：创建具有树形结构的控件（GxtComponentMetaModel为树形结构）</li>
	 * <li>方法作者：花宏宇</li>
	 * <li>编写日期：2010-8-28；时间：下午上午10:53:22</li>
	 * </ul>
	 * <ul>
	 * <b>修订编号：</b>
	 * <li>修订日期：</li>
	 * <li>修订作者：</li>
	 * <li>修订原因：</li>
	 * <li>修订内容：</li>
	 * </ul>
	 * @param <COMPONENT>
	 * @param gxtComponentMetaModelList
	 * @return
	 */
	
	private static <CONTROLLER extends Controller> CONTROLLER createTreeGxtComponentController(
			GxtComponentControllerMetaModel gxtComponentControllerMetaModel) {
		
		//Map<GxtComponentMetaModel, COMPONENT> map = new HashMap<GxtComponentMetaModel, COMPONENT>();
		if(gxtComponentControllerMetaModel.getGxtControllerMetaModelTypeEnum()!=GxtMetaModelTypeEnum.TREE_GXT_CONTROLLER_META_MODEL_TYPE){
			MyLoggerUtil.printError(className, "gxtComponentControllerMetaModel.getGxtControllerMetaModelTypeEnum()!=GxtMetaModelTypeEnum.TREE_GXT_CONTROLLER_META_MODEL_TYPE");
			return null;
		}
		
		//获取根节点
		GxtComponentControllerMetaNodeModel root= gxtComponentControllerMetaModel.getRoot();
		//存放遍历后，创建的控件
		Map<GxtComponentControllerMetaNodeModel, CONTROLLER> map = new HashMap<GxtComponentControllerMetaNodeModel, CONTROLLER>();
				
		GxtComponentControllerMetaNodeModel tempNode = null;
		tempNode = root;
		
		CONTROLLER tempController = null;
		//先序遍历树形结构
		while(tempNode!=null){
			  		//为当前节点创建对象（及赋值），并把它和对应的node，放入map
					tempController = createBaseGxtComponentController(tempNode);
					
			  		map.put(tempNode, tempController);
			  		//如当前节点存在父节点（即父节点为容器），则把当前节点加入到父节点之中
			  		if (gxtComponentControllerMetaModel.getParent(tempNode)!=null) {
			  			
						//((LayoutContainer)map.get(gxtComponentMetaModel.getParent(tempNode))).add(tempComponent);
						GxtComponentControllerMetaNodeModel parent	= gxtComponentControllerMetaModel.getParent(tempNode);
						CONTROLLER parentController = (CONTROLLER)map.get(parent);
						parentController.addChild(tempController);
						
						//MyLoggerUtil.printDebug(className, "控制器"+tempController+"的父控制器是"+((BaseController)tempController).getParent());
					}
			  		
			  	    //对控制器，进行初始化设置
			  		//一般情况下 ,都是创建控制器对应的控件
			  		ControllerFactoryUtil.reConfigureBaseController((BaseController)tempController);
			  		//获得当前节点的下一个节点
			  		tempNode = gxtComponentControllerMetaModel.getNextNode(tempNode);		  	
			  		
		}
		//渲染windowOfWindowContorller
//		ControllerFactoryUtil.renderWindowOfWindowContorller((BaseController)map.get(root));
		ControllerFactoryUtil.renderAndSaveWindowOfWindowContorller(gxtComponentControllerMetaModel,
				(BaseController)map.get(root));
		
		
		//返回添加好各个控件的最终容器
		return map.get(root);
	}
	
	
}
